package com.study.study.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

/**
 * 学习Redis
 *
 * @author zhang
 * @date 2019/03/26
 */
public class RedisClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisClient.class);

    private static final String IP = "192.168.0.15";
    private static final int PORT = 6379;
    private static final String PASSWORD = "root";

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                Jedis jedis = new Jedis(IP, PORT);
//                String replyCode = jedis.auth(PASSWORD);
//                LOGGER.info("认证结果：" + replyCode);
                LOGGER.info("获取初始化的键address的值：" + jedis.get("address"));
                LOGGER.info("删除键address的结果：" + jedis.del("address"));
                LOGGER.info("删除键address后获取的值是：" + jedis.get("address"));
                String redisScript = "if redis.call('exists','address') == 0 then" +
                        "   redis.call('set','address','zhangyi_home') return 'success'" +
                        "else " +
                        "   return 'fail'" +
                        "end";
                Object ret = jedis.eval(redisScript);
                LOGGER.info("执行完lua脚本返回的值是：" + ret);
//                LOGGER.info("执行完lua脚本获取的键值是：" + jedis.get("address"));
                jedis.close();
            }, i + "").start();
        }


//        LOGGER.info("认证结果{}确认服务正在运行:" + jedis.ping(),replyCode);
//
//        LOGGER.info(jedis.get("张毅"));
//
//        HashMap<String, String> map = new HashMap<>(16);
//
//        map.put("1", "1");
//        map.put("0", "1");
//
//        String result = jedis.hmset("李欢", map);
//
//        LOGGER.info(result);
//
//        LOGGER.debug(result + "22");
//
//        LOGGER.info(result + "22");
//
//        List<String> list = jedis.hmget("李欢", "1","0");
//
//        jedis.lpush("list","1","2");
//
//
//        System.out.println(jedis.lpop("list"));
//        list.forEach(System.out::println);
//
//        User user = new User();
//        user.setUserName("zhangyi");
//        jedis.set("user", String.valueOf(user));
//        System.out.println(jedis.get("user"));


//        try {
//            FileOutputStream fos = new FileOutputStream("d:\\temp.out");
//            ObjectOutputStream oos = new ObjectOutputStream(fos);
//            oos.writeObject(user);
//            oos.flush();
//            oos.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        try {
//            FileInputStream fis = new FileInputStream("d:\\temp.out");
//
//            BufferedInputStream bis = new BufferedInputStream(fis);
//            int b;
//            while ((b = bis.read()) != -1){
//                System.out.print((char)b);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//        }


    }
}
